<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Proxy基本</title>
</head>
<body>
<script>
    const emperor = {name: 'Samuel'};
    const representative = new Proxy(emperor, {
        get(target, key) {
            console.log('reading ' + key + ' through a proxy');
            return key in target ? target[key] : 'Emperor don\'t hava this key';
        },
        set(target, key, value) {
            console.log('writing ' + key + ' through a proxy');
            target[key] = value
        }
    })
    // console.log(emperor.name);
    console.log(representative.name); // 通过代理访问，触发get方法
    representative.nickName = 'ZhangBoy'; // 通过代理增加属性，触发set方法
    console.log(emperor.nickName);
</script>
</body>
</html>
